<template>
    <div class="app-container">
        <uc-table url="/admin/fireAlarmInfo/getFireAlarmInfoPage" name="火情预警" ref="fireAlarminfoTable" pageable table-border :search-form="searchForm">
            <template slot="searchForm">
                <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline filter-container">
                    <!--搜索输入框-->
                    <el-form-item>
                        <el-input type="text" v-model="searchForm.name" placeholder="请输入预警名称" clearable @clear="clearInput()"></el-input>
                    </el-form-item>
                    <el-form-item v-if="enterPriseHidden">
                        <el-select v-model="searchForm.enterpriseId" placeholder="请选择主体" style="width: 100%"  clearable @clear="clearInput2()">
                            <el-option v-for="(item,index) in enterpriseDominantList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-select v-model="searchForm.equipmentId" placeholder="请选择设备" style="width: 100%" clearable @clear="clearInput1()" >
                            <el-option v-for="(item,index) in equipmentList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                        </el-select>
                    </el-form-item>
                    <!--搜索按钮--新增按钮-->
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch()">查询</el-button>
<!--                        <el-button type="warning" icon="el-icon-plus" class="filter-item" @click="show()">预警测试</el-button>-->
                    </el-form-item>
                </el-form>
            </template>
            <template slot="columns">
                <el-table-column prop="enterpriseName" label="所属主体" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="name" label="预警名称" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="equipmentName" label="设备名称 " align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="alarmTime" label="预警时间" align="left" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        {{scope.row.alarmTime |dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column prop="alarmInfo" label="预警内容 " align="left" :show-overflow-tooltip="true"></el-table-column>
<!--                <el-table-column prop="location" label="详细地址" align="left" :show-overflow-tooltip="true"></el-table-column>-->
                <el-table-column prop="address" label="操作" width="240" align="left" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <!--编辑按钮-->
<!--                        <el-button size="mini"  :disabled="scope.row.inspectionVodie==null" icon="el-icon-edit" type="primary" @click="handleVideo(scope.row)">查看视频</el-button>-->
                        <el-button size="mini" icon="el-icon-info" type="success" @click="handleInfo(scope.row)">详情</el-button>
                    </template>
                </el-table-column>
            </template>
        </uc-table>
        <el-dialog title="巡检视频" :visible.sync="videoDialogVisible" width="60%">
            <video :src="videoUrl"  controls="controls" autoplay width="100%" height="450px"></video>
        </el-dialog>
        <el-dialog title="预警测试" :visible.sync="hidden">
            <div class="form" >
                <el-form :model="infoForm" ref="infoForm" label-width="100px" :rules="alarmInfoFormRules">
                    <el-form-item label="所属主体" prop="enterpriseId">
                        <el-select v-model="infoForm.enterpriseId" placeholder="请选择主体" style="width: 100%"  @change="getEquipmentList" clearable @clear="clearInput2()">
                            <el-option v-for="(item,index) in enterpriseDominantList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="设备" prop="equipmentId">
                        <el-select v-model="infoForm.equipmentId" placeholder="请选择设备"
                                   style="width: 100%">
                            <el-option v-for="(item,index) in equipmentList" :key="index"  :value="item.ucId" :label="item.disPlayName"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="详细地址" prop="location">
                        <el-input v-model="infoForm.location" placeholder="详细地址" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="预警内容" prop="alarmInfo">
                        <el-input v-model="infoForm.alarmInfo" placeholder="请输入预警内容" clearable></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" style="text-align: center" class="dialog-footer">
                    <el-button type="primary" @click="hidden=false">取消测试
                    </el-button>
                    <el-button type="primary" @click="SubmitText">开始测试
                    </el-button>
                </div>
            </div>

        </el-dialog>

    </div>

</template>

<script>

import UcTable from "@/components/UcTable/index.vue";
import {addInFireAlarmInfo, deleteInFireAlarmInfo} from "@/api/fireAlarmInfo";
import {getInEnterpriseList} from "@/api/enterprise";
import {filterAsyncRoutes} from "@/store/modules/permission";
import {getEquipmentList} from "@/api/equipment";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {getChangeUrl} from "@/utils";


export default {
    name: "fireAlarmInfoList",
    components: {UcTable},
    data(){
        return{
            searchForm:{
                deleted:false,
            },
            deleteLoading:false,
            enterpriseDominantList:[],
            hidden:false,
            infoForm:{

            },
            alarmInfoFormRules:{
                enterpriseId: [{required: true,message:"请选择所属企业", trigger: 'change' } ],
                equipmentId:[{required: true, message: '请选择所属建筑', trigger: 'blur' } ],
                location:[{required: true, message: '请输入详细地址', trigger: 'blur' } ],
                alarmInfo:[{required: true, message: '请输入所属建筑', trigger: 'blur' } ]
            },
            videoDialogVisible:false,
            videoUrl:'',
            equipmentList:{},

        }
    },
  computed:{
    enterPriseHidden(){
      let roleCode = this.$store.getters.roleCode;
      return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"
    }
  },
    created() {
        this.getInEnterpriseDominantList()

    },
    methods:{
        handleVideo (val){
            this.videoUrl = getChangeUrl(val.inspectionVodie)
          console.log(this.videoUrl);
            this.videoDialogVisible = true
        },
        handleSearch(){
            this.$refs.fireAlarminfoTable.getTableData(1)
        },
        show(){
            this.hidden=true
            this.infoForm ={}
            this.$nextTick(()=>{
                this.$refs['infoForm'].clearValidate();
            })
        },
       SubmitText(){
            this.$refs['infoForm'].validate(valid => {
                if (valid) {
                    addInFireAlarmInfo(this.infoForm).then(res => {
                        this.$notify.success({
                            title: '成功',
                            message: "测试成功"
                        })
                        this.hidden=false
                        this.handleSearch()
                    }).catch(res => {
                        this.$notify.error({
                            title: '测试失败',
                            message: res.msg
                        })
                    })
                } else {
                    console.log("表单验证不通过")
                }
            })

        },
        handleEdit(val){
            this.$router.push("/fireAlarm/infoEdit/"+val.ucId)
        },
        handleInfo(val){
            this.$router.push("/fireAlarm/infoInfo/"+val.ucId)
        },
        clearInput(){
            //清除输入框
            this.searchForm.name = null
            this.handleSearch()
        },
        clearInput2(){
            //清除设备下拉框
            this.searchForm.enterpriseId = null
            this.handleSearch()
        },
        clearInput1(){
            //清除设备下拉框
            this.searchForm.equipmentId = null
            this.handleSearch()
        },
        getInEnterpriseDominantList(){
            getInEnterpriseDominantList({deleted:false}).then(res=>{
                this.enterpriseDominantList = res.data
            }).catch(error=>{

            })
        },
        handleDelete(val){
            this.$confirm('您确定要执行删除'+val.name+'这个预警信息吗？', '提醒信息', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.deleteLoading = true
                deleteInFireAlarmInfo(val).then(res=>{
                    this.deleteLoading = false
                    this.$notify.success({
                        title: '成功',
                        message: "删除预警信息成功"
                    })
                    this.handleSearch()
                }).catch(res=>{
                    this.deleteLoading = false
                    this.$notify.error({
                        title: '失败',
                        message: res.msg
                    })
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        getEquipmentList(val){
            getEquipmentList({enterpriseId:val, deleted:false}).then(res=>{
                console.log(res.data)
                this.equipmentList = res.data
            }).catch(error=>{

            })
        },
    }
}
</script>

<style scoped>

</style>
